//
//  HeartRateBarChartView.swift
//  DaRings
//
//  Created by 易先生 on 2022/11/14.
//

import MYModule
import RxCocoa
import RxSwift

class HeartRateBarChartView: View {
    
    var date: Date?
    
    var heartAvg: Int? {
        
        didSet {
            
            guard let heartAvg = heartAvg else {
                dataLabel.text = "--"
                return
            }
 
            
            dataLabel.text = heartAvg.normalTitle
        }
        
    }
    
    var dateString: String = "--" {
    
        didSet {
            timeLabel.text = dateString
        }
        
    }
    
    var type: HeartType = .day
    
    let labelStackView = StackView()
    
    

    override func makeUI() {
        super.makeUI()
        
        let dataStack = StackView()
        dataStack.axis = .horizontal
        dataStack.alignment = .lastBaseline
        dataStack.contentMode = .center
        dataStack.addArrangedSubview(dataLabel)
        dataStack.addArrangedSubview(unitLabel)
        
        labelStackView.axis = .vertical
        labelStackView.alignment = .center
        
        labelStackView.addArrangedSubview(dataStack)
        labelStackView.addArrangedSubview(timeLabel)
        
        workoutsTypeView.titleLabel.text = "heart_rate_workouts".language
        workoutsTypeView.icon.backgroundColor = UIColor.GPSTraining.theme
        sleepingTypeView.titleLabel.text = "heart_rate_sleeping".language
        sleepingTypeView.icon.backgroundColor = UIColor.Sleep.theme
        dayTimeTypeView.titleLabel.text = "heart_rate_day_time".language
        dayTimeTypeView.icon.backgroundColor = UIColor.HeartRate.theme
        
        self.addSubview(labelStackView)
        self.addSubview(typeStackView)
        self.addSubview(barChartView)
        
        
        labelStackView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.centerX.equalToSuperview()
        }
        
        typeStackView.snp.makeConstraints { make in
            make.top.equalTo(labelStackView.snp.bottom).offset(20)
            make.centerX.equalToSuperview()
        }
        
        barChartView.snp.makeConstraints { make in
            make.top.equalTo(typeStackView.snp.bottom).offset(20)
            make.leading.equalToSuperview().offset(6)
            make.trailing.equalToSuperview().offset(-20)
            make.bottom.equalToSuperview().offset(-20)
        }
        
    
        bind()
    }
    
    
    public func bind(to type: HeartType,
                     barDatas: [HeartBarData],
                     YAxisMaxValue: Int,
                     YAxisMinValue: Int = 0,
                     YAxisLineCount: Int) {
        
        self.type = type
        
        switch type {
        case .day:
            
            typeStackView.isHidden = false
            barChartView.chartView.barWidth = 5.0
            let titles = ["00:00",
                         "06:00",
                         "12:00",
                         "18:00",
                         "24:00"]
            
            barChartView.bind(barDatas: barDatas, horizontalTitles: titles, YAxisMaxValue: YAxisMaxValue, YAxisMinValue: YAxisMinValue, YAxisLineCount: YAxisLineCount)
        case .week:
            
            // TODO: 月份无法完美算出刻度，推荐周月都是用charts
            
            typeStackView.isHidden = true
            barChartView.chartView.barWidth = 8.0
            let titles = ["unit_week_full_1".language,
                         "unit_week_full_2".language,
                         "unit_week_full_3".language,
                         "unit_week_full_4".language,
                         "unit_week_full_5".language,
                         "unit_week_full_6".language,
                         "unit_week_full_7".language]
            barChartView.horizontalView.titles = titles
            barChartView.bind(barDatas: barDatas, horizontalTitles: titles, YAxisMaxValue: YAxisMaxValue, YAxisMinValue: YAxisMinValue, YAxisLineCount: YAxisLineCount)
            
        case .month:
            typeStackView.isHidden = true
            barChartView.chartView.barWidth = 5.0
            // TODO: 月份无法完美算出刻度，推荐周月都是用charts
            
            guard let date = date else {
                return
            }
            
            let titles = (1 ... date.monthDays).compactMap { i ->String? in
                
                if i%5 == 0 && i == date.monthDays {
                    return "\(i)"
                }
                
                return nil
            }
          
            barChartView.bind(barDatas: barDatas, horizontalTitles: titles, YAxisMaxValue: YAxisMaxValue, YAxisMinValue: YAxisMinValue, YAxisLineCount: YAxisLineCount)
            
 
        }
    }
    
    
    public func slider(_ point: CGPoint) {
        
        let chartPoint = self.convert(point, to: barChartView)
        barChartView.slider(chartPoint)
        
    }
    
    func bind() {
        
        barChartView.selectIndex.subscribe(onNext: {[weak self] index in
            
            
            guard let self = self, let index = index else {
                
                self?.dataLabel.text = self?.heartAvg?.normalTitle ?? "--"
                return
    
            }
            
            guard let barData = self.barChartView.chartView.barDatas?[index] else {
                self.dataLabel.text = "--"
                return
            }
            
            self.dataLabel.text = barData.avg.normalTitle

        }).disposed(by: rx.disposeBag)
        
    }


    public lazy var dataLabel: Label = {
        let label = Label.init()
        label.text = "--"
        label.font = Fonts.systemFont(ofSize: 40, weight: .semibold)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    public lazy var timeLabel: Label = {
        let label = Label.init()
        label.text = "--"
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    public lazy var unitLabel: Label = {
        let label = Label.init()
        label.text = "heart_rate_bpm".language
        label.font = Fonts.systemFont(ofSize: 11)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    public lazy var workoutsTypeView = SleepHeartTypeView()
    public lazy var sleepingTypeView = SleepHeartTypeView()
    public lazy var dayTimeTypeView = SleepHeartTypeView()
    
    public lazy var typeStackView: StackView = {
        
        
        let stackView = StackView()
        stackView.alignment = .center
        stackView.axis = .horizontal
        stackView.distribution = .equalCentering
        stackView.spacing = 70
        
//        stackView.addArrangedSubview(workoutsTypeView)
        stackView.addArrangedSubview(sleepingTypeView)
        stackView.addArrangedSubview(dayTimeTypeView)
        
        return stackView
        
    }()
    
    public lazy var barChartView: HeartRateChartView = {
        let barChartView = HeartRateChartView()
        return barChartView
    }()
}
